import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

public class Recorder {
    static private BufferedWriter bw = null;
    // 记录文件
    static private String recordPath = "src/recorder.txt";
    // 击毁的坦克数
    static private int allEnemyTankNum = 0;
    // 敌人坦克
    static Vector<Enemy> enemies = new Vector<>();
    // 自己的坦克
    static Hero hero = null;

    public static void setEnemies(Vector<Enemy> enemies) {
        Recorder.enemies = enemies;
    }
    public static void setHero(Hero hero) {
        Recorder.hero = hero;
    }

    public static String getRecordPath() {
        return recordPath;
    }

    public int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }
    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }
    // 击毁一个坦克,数字就应该+1
    public void addEnemyTankNum(){
        allEnemyTankNum++;
    }

    // 保存敌人坦克数量和位置,遍历敌人数量
    // 游戏退出,保存数据
    public static void keepRecord(){
        try {
            bw = new BufferedWriter(new FileWriter(recordPath));
            bw.write(allEnemyTankNum+"\r\n");
            // 保存坦克位置
            for (int i = 0; i < enemies.size(); i++) {
                // 取出敌人坦克,保存横纵坐标
                Enemy enemy = enemies.get(i);
                if (enemy.isLive){
                    String record = enemy.getX() + " " + enemy.getY() + " " + enemy.getDirect() + " " + "1";
                    bw.write(record+"\r\n");
                }
            }
            // 保存自己的坦克
            if (hero.isLive){
                String record = hero.getX() + " " + hero.getY() + " " + hero.getDirect() + " " + "0";
                bw.write(record+"\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bw != null){
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
